#include "Zombie/FlagZombie.h"
#include "AudioEngine.h"

bool FlagZombie::is_run_frame = false;

FlagZombie::FlagZombie() {
	//类属性初始化
	this->zombie_init();
	this->init_zombie_move_animation();
	this->init_zombie_eat_animation();
	this->init_zombie_wait_animation();
	sprite = Sprite::create();
	sprite->setScale(1.6f);
	sprite->runAction(RepeatForever::create(zombie_wait_animate));
	is_run_frame = true;
	//执行调度器
	this->scheduleUpdate();
}


void FlagZombie::update(float update_time) {
	time_count += update_time;
	//auto eat_sound = AudioEngine::play2d("Music/chomp.ogg", true);
	if (is_dead) {
		this->unscheduleUpdate();
		removeFromParent();
	}
}//调度函数  参数调度时间间隔


void FlagZombie::zombie_init() {
	this->zombie_id = -1; // 僵尸编号 
	this->blood = 10;	// 血量
	this->attack_time = 2.0;// 间隔攻击时间
	this->time_count = 0.0;// 时间计时器  统计使用精灵 到结束
	this->attack_num = 100; //攻击值
	this->move_rate = 0; // 移速 相对于移动时间
	this->zombie_type = ZombieType::zombie_type_flag; //僵尸类型
	this->is_move = false;//僵尸是否处于行走状态
	this->is_eat = false;//僵尸是否处于攻击状态
	this->is_dead = false;//僵尸是否已经死亡
	this->is_wait = false;//僵尸是否处于等待进攻状
}// 变量初始化

void FlagZombie::init_zombie_move_animation() {
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ZomBies/FlagZombie/FlagZombie/FlagZombie_default.plist");
	zombie_move = getAnimation("FlagZombie%d.png", 12); // 调用加载图片
	//sprite = Sprite::createWithSpriteFrame(frames.front());
	auto animation1 = Animation::createWithSpriteFrames(zombie_move, 0.1f); // 缓存时间 约小则加载越快  
	zombie_move_animate = Animate::create(animation1);
} // 戴头盔时的移动

void FlagZombie::init_zombie_eat_animation() {
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ZomBies/FlagZombie/FlagZombieAttack/FlagZombieAttack_default.plist");
	zombie_eat = getAnimation("FlagZombieAttack%d.png", 11); // 调用加载图片
	//sprite = Sprite::createWithSpriteFrame(frames.front());
	auto animation1 = Animation::createWithSpriteFrames(zombie_eat, 1.0f / 5); // 缓存时间 约小则加载越快  
	zombie_eat_animate = Animate::create(animation1);
} // 戴头盔的吃

void FlagZombie::init_zombie_wait_animation() {
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("ZomBies/FlagZombie/FlagZombie/FlagZombie_default.plist");
	zombie_wait = getAnimation("FlagZombie%d.png", 3); // 调用加载图片
	//sprite = Sprite::createWithSpriteFrame(frames.front());
	auto animation1 = Animation::createWithSpriteFrames(zombie_wait, 1.0f); // 缓存时间 约小则加载越快  
	zombie_wait_animate = Animate::create(animation1);
}

Vector<SpriteFrame*> FlagZombie::getAnimation(const char* format, int count) {
	auto spritecache = SpriteFrameCache::getInstance();
	Vector<SpriteFrame*> animFrames;
	char str[100];
	for (int i = 1; i <= count; i++) {
		sprintf(str, format, i);
		animFrames.pushBack(spritecache->getSpriteFrameByName(str));
	}
	return animFrames;
}